package com.dx.as.receiver;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.widget.Toast;
import com.dx.as.common.sys.NetworkUtil;

/**
 * Created by jwaoo on 2017/6/19.
 */

public class NetWorkStateReceiver extends BroadcastReceiver{

    @Override
    public void onReceive(Context context, Intent intent) {
        ConnectivityManager manager = (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo networkInfo = manager.getActiveNetworkInfo();
        if (networkInfo == null){
            Toast.makeText(context,"网络已经断开...",Toast.LENGTH_LONG).show();
        }else {
            int netClass = NetworkUtil.getNetworkClass(context);
            String type = "WIFI";
            switch (netClass){
                case NetworkUtil.NETWORK_CLASS_WIFI:
                    type = "WIFI";
                    break;
                case NetworkUtil.NETWORK_CLASS_4_G:
                    type = "4G";
                    break;
                case NetworkUtil.NETWORK_CLASS_3_G:
                    type = "3G";
                    break;
                case NetworkUtil.NETWORK_CLASS_2_G:
                    type = "4G";
                    break;
            }
            Toast.makeText(context,"已连接"+type+"网络",Toast.LENGTH_LONG).show();
        }
    }
}
